package com.xuxueli.applyModules.utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;

import javax.activation.MimetypesFileTypeMap;
import java.io.*;

/**
 * @CLassName ResourceUtil
 * @Description 下载资源
 **/
@Slf4j
public class ResourceUtil {

    /**
     * 下载resources下文档
     */
    public static void downloadDocument(String classPath, OutputStream out) {
        ClassPathResource resource = new ClassPathResource(classPath);
        try (BufferedInputStream in = new BufferedInputStream(resource.getInputStream())) {
            byte[] bytes = new byte[1024];
            int len;
            while ((len = in.read(bytes)) != -1) {
                out.write(bytes, 0, len);
            }
            out.flush();
            out.close();
        } catch (IOException e) {
            log.error("下载失败: {}", classPath);
        }
        log.info("下载成功: {}", classPath);
    }


    /**
     * 下载resources下文档
     */
    public static void downloadFile(String classPath, OutputStream out) throws FileNotFoundException {
        InputStream resource = new FileInputStream(new File(classPath));
        try (BufferedInputStream in = new BufferedInputStream(resource)) {
            byte[] bytes = new byte[1024];
            int len;
            while ((len = in.read(bytes)) != -1) {
                out.write(bytes, 0, len);
            }
            out.flush();
            out.close();
        } catch (IOException e) {
            log.error("下载失败: {}", classPath);
        }
        log.info("下载成功: {}", classPath);
    }


    public static String getcontentType(String fileName){
        String contentType = new MimetypesFileTypeMap().getContentType(fileName);
        return contentType;
    }

    public static void main(String[] args) {
        System.out.println(getcontentType("Daytemplate.docx"));
    }


}

